
//? 单次
function accurateTimeout(time) {
    const date = Date.now()
    return setTimeout(() => {
        console.log('1')
    },time - (Date.now() - date))
}

// accurateTimeout(2000)

//? 递归执行
function accurateTimeoutMany(time,fn) {
    let date = Date.now()
    let timer = null
    let n = 0
    function task(...args) {
        //? 让date要变的
        date += time
        timer = setTimeout(() => {
            fn.apply(args)
            task()
        },date - Date.now())
    }
    task(n++)
    return () => clearTimeout(timer)
}
const end = accurateTimeoutMany(1000,console.log)
setTimeout(() => {
    end()
},4000)
